package com.corpdata.app.contract.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.corpdata.core.base.AbstractBaseService;
import com.corpdata.system.security.shiro.util.UserUtil;
import com.corpdata.app.contract.dao.ContractPartitionMapper;
import com.corpdata.app.contract.entity.ContractPartition;
import com.corpdata.app.contract.entity.ContractStation;
import com.corpdata.app.contract.service.ContractPartitionService;
import com.corpdata.app.contract.service.ContractStationService;
import com.corpdata.app.project.entity.ProjectPartition;
import com.corpdata.app.project.entity.ProjectStation;
import com.corpdata.app.project.service.ProjectStationService;
import com.corpdata.common.result.Result;
import com.corpdata.common.utils.CorpdataUtil;

/**
 * 合同工位关联
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-04 11:12:41
 */
@Service
@Transactional
public class ContractPartitionServiceImpl extends AbstractBaseService<ContractPartition> implements ContractPartitionService {
	
	@Autowired
	private ContractStationService csServ;
	
	@Autowired
	private ProjectStationService psServ;
	
	@Autowired
	private ContractPartitionMapper cpMapper;
	 
	
	public Result insert(ContractPartition record,String fkPartition,String type){
		if(fkPartition!=null){
			ProjectPartition projectPartition=new ProjectPartition();
			projectPartition.setId(fkPartition);
			record.setPartition(projectPartition);
			record.setFlag(0);
			if(type.equals("2")){
				Date date = new Date();
				List<ContractStation> csList = new ArrayList<ContractStation>();
				//封闭空间添加空间下所有工位与合同关联
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("partitionId", fkPartition);
				List<ProjectStation> list = psServ.findAll(params);
				for(int i =0;i<list.size();i++){
					ContractStation cs = new ContractStation();
					cs.setFkContract(record.getFkContract());
					cs.setFkPartition(fkPartition);
					cs.setStation(list.get(i));
					cs.setFlag(0);
					cs.setId(CorpdataUtil.getUUID());
					cs.setCreated(date);
					cs.setModified(date);
					cs.setCreater(UserUtil.getCurrentUserid());
					cs.setModifier(UserUtil.getCurrentUserid());
					csList.add(cs);
				}
				csServ.insertList(csList);
			}
		}
		return super.save(record);
	}

	@Override
	public Result deleteByPartition(String id, String contractId) {
		//删除合同工位关联
		csServ.deleteByPartition(id,contractId);
		return super.deleteById(id);
	}
	
	//续签时将原合同关联加到续签合同中
	@Override
	public void insertContractPartition(String contractId,String id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("contractId", contractId);
		List<ContractPartition> partitions = cpMapper.selectAll(params);
		for(ContractPartition cp: partitions){
			cp.setFkContract(id);
			cp.setFlag(0);
			cp.setId(CorpdataUtil.getUUID());
			super.save(cp);
		}
		//获取合同下合同工位关联
		List<ContractStation> csList = csServ.selectAllByContract(contractId);
		List<ContractStation> list = new ArrayList<ContractStation>();
		for(ContractStation c:csList){
			c.setId(CorpdataUtil.getUUID());
			c.setFkContract(id);
			c.setFlag(0);
			list.add(c);
		}
		if(csList.size()>0){
			csServ.insertList(csList);
		}
	}
	/**
	 * 删除临时数据
	 */
	@Override
	public void delTemporaryData() {
		cpMapper.delTemporaryData();
		
	}

}
